//
//  YYTJController.m
//  FruitsWholesaler
//
//  Created by sgone-2 on 15/11/29.
//  Copyright © 2015年 sgone. All rights reserved.
//

#import "YYTJController.h"
#import "AFNetworking.h"

//动态数字变化
#import "UICountingLabel.h"

@interface YYTJController ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;



//网路请求
@property(nonatomic,strong)AFHTTPSessionManager *manager;
//获取日历的时间
@property(nonatomic,strong) NSString *data;


//订单的数量以及订单的金额
//@property(nonatomic,strong) UILabel *lblNumaer;
//@property(nonatomic,strong) UILabel *lblPrice;

//动态数字变化
@property(nonatomic,strong) UICountingLabel *lblNumaer;
@property(nonatomic,strong) UICountingLabel *lblPrice;


//动态数字。今天营业额
@property(nonatomic,strong) UICountingLabel *tlblNumber;
@property(nonatomic,strong) UICountingLabel *tlblPrice;




//显示日历
@property(nonatomic,strong) UITextField *riliText;





//订单和金额string
@property(nonatomic,strong) NSString *DD;
@property(nonatomic,strong) NSString *JE;
//总称
@property(nonatomic,strong) NSString *zonghe;


////选择日历产生的订单数量以及金额
//@property(nonatomic,strong) UITextField *textDD;


@property(nonatomic,strong) UITextField *lblSHUJU;



@end

@implementation YYTJController



//afnetworking网络请求数据
- (AFHTTPSessionManager *)manager
{
    if (_manager == nil) {
        _manager  = [AFHTTPSessionManager manager];
    }
    return _manager;
    
    
}



- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"营业统计";
    self.backBtn.hidden = NO;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.scrollEnabled = YES;
    //获得当前的时间
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"YYYY-MM-dd"];
    self.data = [formatter stringFromDate:[NSDate date]];
    
    
    //赋予初始值
    self.DD = @"0";
    self.JE = @"0.0";
    
    self.zonghe = [NSString stringWithFormat:@"    全部     下单  %@       金额  %@元",self.DD,self.JE];
  
    [self sendAPI];
    
    
}




//首先获得全部的订单以及金额
#pragma mark 获取网路数据

-(void)sendAPI
{


   
    
    
    //只获得当前的
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"YYYY-MM-dd"];
    NSString *data = [formatter stringFromDate:[NSDate date]];



    
    
    NSString *urlStr = [NSString stringWithFormat:@"%@business_report",SERVER_ADDRESS2];
    
    NSDictionary *params =@{@"wTel" : self.dataHandler.phone , @"uToken" : self.dataHandler.uToken ,@"report_time" : data};
    [self.manager POST:urlStr parameters:params success:^(NSURLSessionDataTask *task, id responseObject) {

        
        if ([responseObject[@"error_flag"] intValue] == 0)
        {
            
            
//            NSLog(@"ttttttttt  %@",responseObject);
            
            
            
           
            //显示全部的价格以及数量
            
            int number =  [[[[responseObject objectForKey:@"data"] objectForKey:@"report"] objectForKey:@"all_order"] intValue];
            self.lblNumaer.method = UILabelCountingMethodLinear;
            self.lblNumaer.format = @"%d";
           [self.lblNumaer countFrom:0 to:number withDuration:0.5];


            
            
            
            //用int代替
            NSString *amount = [[[responseObject objectForKey:@"data"] objectForKey:@"report"] objectForKey:@"all_amount"];
            NSString *amount1 = [NSString stringWithFormat:@"%@",amount ];
            
           
            
            self.lblPrice.method = UILabelCountingMethodLinear;
            self.lblPrice.format = @"%d";
            [self.lblPrice countFrom:0.00 to:[amount1 intValue] withDuration:0.5];

            
            
            
            
//            amount1 = @"909099090.00";
//            
//            self.lblPrice.text = amount1;
//            
//            
//            
//            //lbel自动分行
//            self.lblPrice.numberOfLines = 0;
//            self.lblPrice.lineBreakMode = NSLineBreakByWordWrapping;
//            CGSize size  =[self.lblPrice sizeThatFits:CGSizeMake(self.lblPrice.frame.size.width, 100)];
//            self.lblPrice.frame = CGRectMake(self.lblPrice.frame.origin.x, self.lblPrice.frame.origin.y, self.lblPrice.frame.size.width, size.height);
//            
            
            
            
            //获得今日订单以及今日金额
            
            int number2 =  [[[[responseObject objectForKey:@"data"] objectForKey:@"report"] objectForKey:@"time_order"] intValue];
            self.tlblNumber.method = UILabelCountingMethodLinear;
            self.tlblNumber.format = @"  %d";
            [self.tlblNumber countFrom:0 to:number2 withDuration:0.5];

            
            
            
            NSString *amount4 = [[[responseObject objectForKey:@"data"] objectForKey:@"report"] objectForKey:@"time_amount"];

            self.tlblPrice.format = @"  %.2f元";
            
            [ self.tlblPrice countFrom:0.00 to:[amount4 floatValue] withDuration:0.5];
            
            
            
            
            
            //显示下一个section  cell的数据
            self.DD = [[[responseObject objectForKey:@"data"] objectForKey:@"report"] objectForKey:@"time_order"] ;
            
            self.JE =  [[[responseObject objectForKey:@"data"] objectForKey:@"report"] objectForKey:@"time_amount"];

            
            
            
            //刷新指定的section
            NSIndexSet *indexSet = [[NSIndexSet alloc]initWithIndex:1];
            [self.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];
           
            
            
            
//            [self.tableView reloadData];
            

            
            
            
            
        }
        
        else
        {
        
            NSString *str = [NSString stringWithFormat:@"%@",responseObject[@"result_msg"]];
            UIAlertView *alter = [[UIAlertView alloc]initWithTitle:@"提示" message:str delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
            [alter show];

        
        
        
        
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        UIAlertView *alter = [[UIAlertView alloc]initWithTitle:@"提示" message:@"请检查网络!" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [alter show];
        
        
    }];
    
    



}






-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    return 2;

}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{


    return 1;

}





-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{

    if (section == 0) {
        return 220;
    }
    else
    {
    
        return 70;
    
    }

}




-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{

    if (section == 0) {
        
        return 0.001;
    }

    else
    {
    
        return 100;
    
    }

}




-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{


    UIView *view2 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 10)];
    [self.view addSubview:view2];
    
    
    if (section == 0) {
    
    
    view2.backgroundColor = BenSe_COLOR;

    UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(15, 0, 80, 40)];
    lbl.text = @"全部";
    lbl.textColor = [UIColor whiteColor];
    lbl.font = [UIFont systemFontOfSize:18];
    [view2 addSubview:lbl];
    
    
    
    //订单1
    UILabel *lbl1 = [[UILabel alloc]initWithFrame:CGRectMake(15, 50, 40, 40)];
    lbl1.textColor = [UIColor whiteColor];
    lbl1.text = @"订单";
    lbl1.font = [UIFont systemFontOfSize:18];
    [view2 addSubview:lbl1];
    
    //订单数量
    
    self.lblNumaer = [[UICountingLabel alloc]initWithFrame:CGRectMake(55, 50, 150, 40)];
    self.lblNumaer.textColor = [UIColor whiteColor];
    self.lblNumaer.font = [UIFont systemFontOfSize:30];
    [view2 addSubview:self.lblNumaer];
    
    
    
    //金额
    UILabel *lbl2 = [[UILabel alloc]initWithFrame:CGRectMake(150, 50, 100, 40)];
    lbl2.text = @"金额";
    lbl2.font = [UIFont systemFontOfSize:18];
    lbl2.textColor = [UIColor whiteColor];
    [view2 addSubview:lbl2];
    
    
    //订单的金额
    self.lblPrice = [[UICountingLabel alloc]initWithFrame:CGRectMake(192, 50, 132, 40)];
    self.lblPrice.font = [UIFont systemFontOfSize:30];
    self.lblPrice.textColor = [UIColor whiteColor];
    [view2 addSubview:self.lblPrice];
    
    
    self.lblNumaer.text = @"0";
    self.lblPrice.text = @"0.00";
    
    
    //获取所有的数据
    
    [self sendAPI];
    
    
    //今日订单
    //添加蒙版
    UIView *mengBan = [[UIView alloc]initWithFrame:CGRectMake(0, 120, ViewWidth, 100)];
    mengBan.backgroundColor = [UIColor colorWithWhite:1 alpha:0.3];
    [view2 addSubview:mengBan];
    
    
    //添加竖线
    UIView *view10  = [[UIView alloc]initWithFrame:CGRectMake(ViewWidth/2, 20, 1, 60)];
    view10.backgroundColor = BACKGROUND2;
    [mengBan addSubview:view10];
    
    
    
    //今日订单以及今日营业额
    
    
    UILabel *jrdd = [[UILabel alloc]initWithFrame:CGRectMake(ViewWidth/6-10, 50, ViewWidth/6+10, 40)];
    jrdd.textColor = [UIColor whiteColor];
    jrdd.text = @"今日订单";
        jrdd.font = [UIFont systemFontOfSize:15];
    [mengBan addSubview:jrdd];

    
    
    self.tlblNumber = [[UICountingLabel alloc]initWithFrame:CGRectMake(ViewWidth/6-10, 20, ViewWidth/6+40, 40)];
    
    self.tlblNumber.textColor = [UIColor whiteColor];
    self.tlblNumber.font = [UIFont systemFontOfSize:19];
    [mengBan addSubview:self.tlblNumber];

    

    
    
    
    UILabel *yye = [[UILabel alloc]initWithFrame:CGRectMake((ViewWidth/6)*4-10, 50, ViewWidth/6+30, 40)];
    yye.textColor = [UIColor whiteColor];
    yye.text = @"今日营业额";
        yye.font = [UIFont systemFontOfSize:15];
    [mengBan addSubview:yye];
    
    
    
    self.tlblPrice = [[UICountingLabel alloc]initWithFrame:CGRectMake((ViewWidth/6) * 4-10, 20, ViewWidth/6+40, 40)];
    
    self.tlblPrice.textColor = [UIColor whiteColor];
    self.tlblPrice.font = [UIFont systemFontOfSize:19];
    [mengBan addSubview:self.tlblPrice];
    
    
    }
    
    
    
    else
    {
    
    
        UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake((ViewWidth -80)/2 , 25, 80, 44)];
        title.text = @"当日数据";
        title.textAlignment = NSTextAlignmentCenter;
        [view2 addSubview:title];
    
    
    }
    
    
    
    
    
    return view2;


}





-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{




    if (indexPath.section == 0) {
        return 55;
    }
    else
    {
    
        return 88;
    
    }
    
    
}





-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{
    static NSString *CellIdentifier = @"Cell";

//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//    
//    if (cell == nil)
//    {
//   
//        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
//        cell.selectionStyle = UITableViewCellSelectionStyleNone;
//    }
    
    
    

    
    
    if (indexPath.section == 0) {
        
        UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
  
        
        if (indexPath.row == 0) {
            

            
    cell.textLabel.textColor = [UIColor grayColor];
    cell.textLabel.text = @"选择日期";

    
//    if (self.riliText == nil) {
        
         self.riliText = [[UITextField alloc]initWithFrame:CGRectMake(85, 0, ViewWidth-85-15, 55)];
        //自定义rightView的大小
        self.riliText.enabled = NO;
        
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
        imageView.image = [UIImage imageNamed:@"icon_date"];
        
        self.riliText.rightView = imageView;
        [cell addSubview:self.riliText];
        self.riliText.rightViewMode = UITextFieldViewModeAlways;
        self.riliText.textColor = [UIColor lightGrayColor];
        self.riliText.placeholder = @"         点击获取时间";
        self.riliText.text = [NSString stringWithFormat:@"          %@", self.data];
//        [cell addSubview:imageView];
        
        
//        }
            
   
//    self.riliText.rightView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"icon_date"]];
    
    



    
    
    
    //添加button弹出pickView，显示日期操作
    UIButton *pickBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, ViewWidth , 55)];
    [cell addSubview:pickBtn];
    [pickBtn addTarget:self action:@selector(pickAdd:) forControlEvents:UIControlEventTouchUpInside];
            
    }
        
        return cell;
        
        
        
    }
    
    
    
    else  if (indexPath.section == 1)
        
    {
        UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        if (indexPath.row == 0) {
       
        
        
    
        UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(8, 0, ViewWidth - 16, 44)];
        
        lbl.backgroundColor = TiaoColor;
        
        lbl.text = [NSString stringWithFormat:@"  日期 |  %@",self.data];
        
        lbl.textColor = [UIColor whiteColor];
        
        
        
        
        [cell addSubview:lbl];

        
        
        //选择的日期显示的数据
        
            self.lblSHUJU =[[UITextField alloc]initWithFrame:CGRectMake(0, 44, ViewWidth, 44)];
            self.lblSHUJU.textColor = [UIColor grayColor];
            [cell addSubview:self.lblSHUJU];
            self.lblSHUJU.text = self.zonghe;
            self.lblSHUJU.delegate = self;
            self.lblSHUJU.enabled = NO;
            
            

        }
        
        
        
        
        return cell;

    
    
    }
    
    
    else
    {
        
        UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    
    
    }
    
    
    
    


}



#pragma mark pick弹出操作

-(void)pickAdd:(id)sender
{

    [self setupDateView:DateTypeOfStart];


}


- (void)setupDateView:(DateType)type {
    
    _pikerView = [HZQDatePickerView instanceDatePickerView];
    _pikerView.frame = CGRectMake(0, 0, ScreenRectWidth, ScreenRectHeight + 20);
    [_pikerView setBackgroundColor:[UIColor clearColor]];
    _pikerView.delegate = self;
    _pikerView.type = type;
    //    [_pikerView.datePickerView setMinimumDate:[NSDate date]];
    [self.view addSubview:_pikerView];
    
}



- (void)getSelectDate:(NSString *)date type:(DateType)type {
//   NSLog(@"%d - %@", type, date);
    
    switch (type) {
        case DateTypeOfStart:
            
        {
            
            self.data = [NSString stringWithFormat:@"%@",date];
            
                        
            //进行网络传递到服务器进行操作获取网络数据操作
            NSString *urlString = [NSString stringWithFormat:@"%@business_report",SERVER_ADDRESS2];
            NSDictionary *params =@{@"wTel" : self.dataHandler.phone , @"uToken" : self.dataHandler.uToken ,@"report_time" : date};
            
            [self.manager POST:urlString parameters:params success:^(NSURLSessionDataTask *task, id responseObject) {
                if ([responseObject[@"error_flag"] intValue] == 0) {
                
                    
                    
                    
                    
                    //显示下一个section  cell的数据
                    self.DD = [[[responseObject objectForKey:@"data"] objectForKey:@"report"] objectForKey:@"time_order"] ;
                    
                    self.JE =  [[[responseObject objectForKey:@"data"] objectForKey:@"report"] objectForKey:@"time_amount"];
                    
                    
                    self.riliText.text = [NSString stringWithFormat:@"          %@", self.data];
                    
                    self.zonghe = [NSString stringWithFormat:@"    全部     下单  %@       金额  %@",self.DD,self.JE];

                    self.lblSHUJU.text = self.zonghe;
                  
//                    [self.tableView reloadData];
                   
                    
                    
                    //刷新指定的section
                    NSIndexSet *indexSet = [[NSIndexSet alloc]initWithIndex:1];
                    [self.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];
                }
               
                
                
                
            } failure:^(NSURLSessionDataTask *task, NSError *error) {
                
                
            }];
            
            
            
        }
            break;
            

            
        default:
            break;
    }
}




#pragma mark 进行textfield的end赋值编辑操作
-(void)textFieldDidEndEditing:(UITextField *)textField

{
    if (textField == self.lblSHUJU) {
        
        
        self.zonghe = self.lblSHUJU.text;
 
    }


}





@end
